package cn.wolfcode.mq;

import cn.wolfcode.service.ISeckillProductService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.annotation.SelectorType;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName OrderFailMQConsumer
 * @Author VCR
 * @Date 2022/1/3 11:17
 * @Version 1.0
 */

/**
 * 秒杀失败消费者MQ
 */
@Component
@Slf4j
@RocketMQMessageListener(topic=MQConstant.ORDER_RESULT_TOPIC,consumerGroup="OrderFailMQConsumer",selectorExpression=MQConstant.ORDER_RESULT_FAIL_TAG,selectorType = SelectorType.TAG)
public class OrderFailMQConsumer implements RocketMQListener<OrderMQResult> {   // 返回消息为OrderMQResult

    @Autowired
    private ISeckillProductService seckillProductService;   // 注入秒杀商品Service

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public void onMessage(OrderMQResult orderMQResult) {
        log.info("进入失败消息中");
        // 1.回补预库存
        // key:seckillStockCount:time  valueKey:seckillId vv:stockCount
        seckillProductService.replenishRedisStockCount(orderMQResult.getTime(),orderMQResult.getSeckillId());
        // 2.修改本地标识 (使用MQ发送消息) 消息为seckillId
        rocketMQTemplate.sendOneWay(MQConstant.CANCEL_SECKILL_OVER_SIGE_TOPIC,orderMQResult.getSeckillId());
    }
}
